import string

def getSexp(msg):
    """Input: string of an s-expression
    Output: a list of all components of that s-expression"""
    a = []; b = ""
    i = 1
    while i < len(msg):
        if msg[i] == "(" : 
            res = getSexp(msg[i:])
            a.append(res[0])
            i += res[1]
        elif msg[i] == ")":
            if b != "": 
                a.append(b); b=""
            if i == msg.rfind(")") : return a 
            else : return [a,i]
        elif msg[i] == " " and b != "": 
                a.append(b); b=""
        else :  b += msg[i]; b = b.strip()
        i+=1
        
def printSexp(msg):
    """Makes an sexp out of a list or tuple, atom strings cannot contain: ()[]',
    Input: a list of all components of s-expression
	Output: a string of an s-expression"""
    return str(msg).replace("[", "(").replace("]", ")").replace(",", "").replace("'", "")
    
def saneSexp(msg):
    " Checks if there are equal '(' and ')' "
    balance = 0
    for x in msg:
        if x == "(": balance += 1
        if x == ")": balance -= 1
        if balance < 0: return False
    return balance
